package main.java.indi.zyj.c150.pointer;

import java.util.PriorityQueue;

public class _03_MergeKLists {

    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length == 0) return null;
        ListNode head = new ListNode(-1);
        ListNode p = head;
        PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(
                lists.length,
                (a, b) -> (a.val - b.val)
                );
        for (ListNode start : lists){
            if (start != null){
                priorityQueue.add(start);
            }
        }
        while (!priorityQueue.isEmpty()){
            ListNode poll = priorityQueue.poll();
            p.next = poll;
            if (poll.next != null){
                priorityQueue.add(poll.next);
            }
            p = p.next;
        }
        return head.next;
    }

}
